package EGS.impl;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;

import ORDER.c_order;
import ORDER.m_orderBOM;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service100006Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 100006 获取BOM
	 */
	public EGS_execute_service100006Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iLayer;
			int iiLayer;


			if (msgClass.equals("100006")) {// 获取BOM
				String orderNo = null;
				try {
					orderNo = Mroot.element("body").element("orderInfo").attribute("order_no").getValue();// iSN=modelNo
				} catch (Exception e) {
					;
				}
				if (orderNo == null) {
					orderNo = "";
				}
				iLayer = Mroot.element("body").element("orderInfo").attribute("layer").getValue();
				iiLayer = Integer.parseInt(iLayer);

				Element eleB = Mroot.element("body");
				Element eleR = eleB.addElement("result");
				try {
					
					if (orderNo.length() < 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "工单号不能为空");
						AKSLogs(100060, "order=" + orderNo + ":_工单号不能为空");
					} else {
						m_orderBOM result = new c_order(sql).getOrderBOM(orderNo, iiLayer);// 按照工单面别获取BOM

						Element eleBOM = eleB.addElement("BOM");
						if (result.panelNumber != null) {
							if (result.panelNumber.size() > 0) {
								Element elePanels = eleBOM.addElement("panels");
								for (int i = 0; i < result.panelNumber.size(); i++) {
									elePanels.addElement("panel").addAttribute("number", result.panelNumber.get(i));
								}
							}
						}
						if (result.masterNumber != null) {
							if (result.masterNumber.size() > 0) {
								Element masters = eleBOM.addElement("masters");
								for (int i = 0; i < result.masterNumber.size(); i++) {
									Element eleMaster = masters.addElement("master");
									eleMaster.addAttribute("number", result.masterNumber.get(i));
									eleMaster.addAttribute("version", result.masterVersion.get(i));
									String iBIN = "" + result.BSindex.get(i);
									while (iBIN.length() < 3) {
										iBIN = "0" + iBIN;
									}
									eleMaster.addAttribute("BIN", iBIN);
									eleMaster.addAttribute("name", result.masterName.get(i));
								}
							}
						}
						if (result.partNumber != null) {
							if (result.partNumber.size() > 0) {
								Element eleParts = eleBOM.addElement("parts");
								for (int i = 0; i < result.partNumber.size(); i++) {
									Element elePart = eleParts.addElement("part");
									elePart.addAttribute("number", result.partNumber.get(i));
									elePart.addAttribute("version", result.partVersion.get(i));
									elePart.addAttribute("name", result.partName.get(i));
									elePart.addAttribute("pos", result.partPos[i]);
									elePart.addAttribute("master", result.partBABA.get(i));
									elePart.addAttribute("panel", result.partMAMA.get(i));
								}
							}
						}
						if (result.comps != null) {
							if (result.comps.size() > 0) {
								Element eleComps = eleBOM.addElement("comps");
								for (int i = 0; i < result.comps.size(); i++) {
									if (result.comps.get(i).procedure != null
											&& result.comps.get(i).procedure.equals(iClient.myProcedureNumber)) {
										Element eleComp = eleComps.addElement("comp");
										eleComp.addAttribute("part", result.comps.get(i).compBABA);
										eleComp.addAttribute("layer", "" + result.comps.get(i).layer);
										eleComp.addAttribute("pos", result.comps.get(i).name);
										eleComp.addAttribute("number", result.comps.get(i).materialNumber);
										eleComp.addAttribute("name", result.comps.get(i).materialName);
									}
								}
							}
						}

						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						AKSLogs(100061, "order=" + orderNo + "_" + iLayer);
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					AKSLogs(100060, "order=" + orderNo + ":_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
